package DBs;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DBExec {

	public void selectTable(String sql, Syori s, HttpServletRequest req, HttpServletResponse res){
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try{
			DBAccess dba = new DBAccess();

			conn = dba.getConnection();

			rs = dba.execSQLQuery(conn, sql);

			s.syori(rs, req, res);

			conn.commit();

		}catch(SQLException e){
			e.printStackTrace();
			if(conn != null){
				try{
					conn.rollback();
				}catch(SQLException el){
					e.printStackTrace();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			if(conn != null){
				try{
					conn.rollback();
				}catch(SQLException el){
					e.printStackTrace();
				}
			}
		}finally{
			if(rs != null){
				try{
					rs.close();
				}catch(SQLException el){
					rs = null;
				}
			}
			if(stmt != null){
				try{
					stmt.close();
				}catch(SQLException el){
					stmt = null;
				}
			}
			if(conn != null){
				try{
					conn.close();
				}catch(SQLException el){
					conn = null;
				}
			}
		}
	}

	public int updateTable(String sql){
		Connection conn = null;
		Statement stmt = null;
		int l = 0;
		try{
			DBAccess dba = new DBAccess();

			conn = dba.getConnection();

			l = dba.execSQLUpdate(conn, sql);

			conn.commit();

		}catch(SQLException e){
			e.printStackTrace();
			if(conn != null){
				try{
					conn.rollback();
				}catch(SQLException el){
					e.printStackTrace();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			if(conn != null){
				try{
					conn.rollback();
				}catch(SQLException el){
					e.printStackTrace();
				}
			}
		}finally{
			if(stmt != null){
				try{
					stmt.close();
				}catch(SQLException el){
					stmt = null;
				}
			}
			if(conn != null){
				try{
					conn.close();
				}catch(SQLException el){
					conn = null;
				}
			}
		}
		return l;
	}

}
